package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnAuditMapper;
import com.yidu.dao.SnComInDetailMapper;
import com.yidu.dao.SnComInventoryMapper;
import com.yidu.dao.SnComSaDetailMapper;
import com.yidu.dao.SnComSalesMapper;
import com.yidu.dao.SnCommodityMapper;
import com.yidu.dao.SnFiDetailMapper;
import com.yidu.dao.SnFinanceMapper;
import com.yidu.dao.SuWhDetailMapper;
import com.yidu.dao.SuWholesaleMapper;
import com.yidu.model.SnAudit;
import com.yidu.model.SnComInDetail;
import com.yidu.model.SnComSaDetail;
import com.yidu.model.SnComSales;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnFiDetail;
import com.yidu.model.SnFinance;
import com.yidu.model.SnUser;
import com.yidu.model.SuWhDetail;
import com.yidu.model.SuWholesale;
import com.yidu.service.SuWholeSaleService;

/**
 * 批发订单实现类
 * @Type SuWholeSaleServiceImpl
 * @author ZhuYiFan
 * @Date 2018年11月28日
 * @Time 下午1:54:09
 */
@Service
public class SuWholeSaleServiceImpl implements SuWholeSaleService{
	
	/**
	 * 批发订单Mapper注入
	 */
	@Resource 
	private SuWholesaleMapper suWholesaleMapper;
	
	/**
	 * 批发明细Mapper注入
	 */
	@Resource
	private SuWhDetailMapper suWhDetailMapper;
	
	/**
	 * 商品Mapper注入
	 */
	@Resource
	private SnCommodityMapper snCommodityMapper;
	
	/**
	 * 审核Mapper注入
	 */
	@Resource
	private SnAuditMapper snAuditMapper;
	
	/**
	 * 商品库存Mapper注入
	 */
	@Resource
	private SnComInventoryMapper comInventoryMapper;
	
	/**
	 * 商品库存明细Mapper注入
	 */
	@Resource
	private SnComInDetailMapper snComInDetailMapper;
	
	/**
	 * 财务Mapper注入
	 */
	@Resource
	private SnFinanceMapper snFinanceMapper;
	
	/**
	 * 财务记录Mapper注入
	 */
	@Resource 
	private SnFiDetailMapper snFiDetailMapper;
	
	/**
	 * 退货订单Mapper注入
	 */
	@Resource
	private SnComSalesMapper snComSalesMapper;
	
	/**
	 * 退货订单明细Mapper注入
	 */
	@Resource
	private SnComSaDetailMapper snComSaDetailMapper;
	
	@Override
	public List<SuWholesale> findAll(Map<String, Object> maps) {
		return suWholesaleMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		return suWholesaleMapper.findCount(maps);
	}

	@Override
	public int add(String str,String sum,HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//根据#号分割数据成数组string
		String[] string = str.split("#");
		//定义一个变量count
		Integer count = 0;
		//循环数组string
		for (int i = 0; i < string.length; i++) {
			//把数据根据，分割成数组str2
			String[] str2 = string[i].split(",");
			//取到商品数量8
			String number = str2[8];
			//给count赋值
			count += Integer.parseInt(number);
		}
		//创建批发订单对象
		SuWholesale suWholesale = new SuWholesale();
		//给批发ID赋值
		suWholesale.setWhId(Tools.getDateOrderNo());
		//给分公司ID赋值
		suWholesale.setBraId(user.getBraId());
		//给审核状态赋值1
		suWholesale.setWhState(1);
		//根据￥取到总金额
		String sumMoney = sum.split("￥")[0];
		//总金额赋值
		suWholesale.setWhSum(Double.valueOf(sumMoney));
		//是否有效赋值1
		suWholesale.setIsva(1);
		//批发订单时间赋值
		suWholesale.setOptime(Tools.getCurDateTime());
		//批发订单操作人赋值
		suWholesale.setOper(user.getUserName());
		//总数量赋值
		suWholesale.setWhCount(count);
		//批发状态sort赋值1
		suWholesale.setSort(1);
		//调用批发订单mapper增加批发订单的方法返回rows
		int rows = suWholesaleMapper.insertSelective(suWholesale);
		//创建审核对象
		SnAudit snAudit = new SnAudit();
		//给审核ID赋值
		snAudit.setAuId(Tools.getDateOrderNo());
		//给审核订单ID赋值
		snAudit.setAllId(suWholesale.getWhId());
		//给审核状态赋值1
		snAudit.setAuState(1);
		//调用审核mapper增加审核的方法返回row
		int row = snAuditMapper.insertSelective(snAudit);
		//定义一个变量rowa
		int rowa = 0;
		//循环数组string
		for (int i = 0; i < string.length; i++) {
			//把数据根据，分割成数组str2
			String[] str2 = string[i].split(",");
			//得到商品ID0
			String comId = str2[0];
			//得到商品价格4
			String money = str2[4];
			//得到商品总价6
			String all = str2[6];
			//得到商品数量8
			String number = str2[8];
			//创建批发详情对象
			SuWhDetail suWhDetail = new SuWhDetail();
			//给批发详情ID赋值
			suWhDetail.setWdId(Tools.getDateOrderNo());
			//给商品ID赋值
			suWhDetail.setComId(comId);
			//给批发订单ID赋值
			suWhDetail.setWhId(suWholesale.getWhId());
			//给金额赋值
			suWhDetail.setWdMoney(Double.valueOf(money));
			//给数量赋值
			suWhDetail.setWdNumber(Integer.valueOf(number));
			//给总金额赋值
			suWhDetail.setWdSum(Double.valueOf(all));
			//是否有效赋值1
			suWhDetail.setIsva(1);
			//给时间赋值
			suWhDetail.setOptime(Tools.getCurDateTime());
			//给操作人赋值
			suWhDetail.setOper(user.getUserName());
			//调用批发订单详情mapper增加销售详情的方法返回rowa
			rowa = suWhDetailMapper.insertSelective(suWhDetail);
		}
		//如果全部成功返回rows否则返回0
		if(row > 0 && rows > 0 && rowa > 0){
			return rows;
		}else{
			return 0;
		}
	}

	@Override
	public Integer queryInventory(String comId,String braId) {
		return suWholesaleMapper.queryInventory(comId,braId);
	}

	@Override
	public List<SnAudit> findWholesale(Map<String, Object> maps) {
		return snAuditMapper.findWholesale(maps);
	}

	@Override
	public int findWholesaleCount(Map<String, Object> maps) {
		return snAuditMapper.findWholesaleCount(maps);
	}

	@Override
	public int auditWholesale(SnAudit snAudit,HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//给操作人赋值
		snAudit.setOper(user.getUserName());
		//给操作时间赋值
		snAudit.setOptime(Tools.getCurDateTime());
		//调用审核mapper批发订单审核的方法返回rows
		int rows = snAuditMapper.auditWholesale(snAudit);
		//调用批发订单mapper修改批发订单状态的方法返回row2
		int rows2 = suWholesaleMapper.updateState(snAudit);
		//如果全部成功返回rows否则返回0
		if(rows > 0 && rows2 >0){
			return rows;
		}else{
			return 0;
		}
	}

	@Override
	public int confirmWholesale(String whId,HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//调用批发订单mapper修改批发状态(排序)的方法
		int row = suWholesaleMapper.updateSort(whId);
		//调用批发订单详情mapper根据批发订单ID查询批发商品明细
		List<SuWhDetail> list = suWhDetailMapper.findComId(whId);
		//定义变量rows
		int rows = 0;
		//定义变量rows2
		int rows2 = 0 ;
		//定义总金额变量moneys
		double moneys = 0.0;
		//循环查询的数据
		for (SuWhDetail suWhDetail : list) {
			//调用商品库存mapper根据分公司ID和商品Id修改商品库存的方法返回rows
			rows = comInventoryMapper.updateInventory(user.getBraId(),suWhDetail.getComId(),suWhDetail.getWdNumber());
			//调用商品库存mapper根据分公司ID和商品ID查询商品库存ID的方法
			String inventoryId = comInventoryMapper.queryInventoryId(user.getBraId(),suWhDetail.getComId());
			//创建库存明细对象
			SnComInDetail snComInDetail = new SnComInDetail();
			//给库存ID赋值
			snComInDetail.setCiId(inventoryId);
			//给库存明细赋值
			snComInDetail.setCidExplain("批发"+suWhDetail.getComName()+"减少"+suWhDetail.getComName()+"库存"+suWhDetail.getWdNumber()+suWhDetail.getComUnit());
			//给是否有效赋值1
			snComInDetail.setIsva(1);
			//给操作时间赋值
			snComInDetail.setOptime(Tools.getCurDateTime());
			//调用商品库存明细mapper增加库存明细的方法返回rows2
			rows2 = snComInDetailMapper.insertSelective(snComInDetail);
			//所有商品小计赋值给总金额
			moneys+=suWhDetail.getWdSum();
		}
		//调用财务mapper根据分公司ID修改总财务金额的方法返回rows3
		int rows3 = snFinanceMapper.updateMoney(user.getBraId(),moneys);
		//调用财务mapper根据分公司ID查询财务ID的方法
		SnFinance snFinance = snFinanceMapper.findByFinId(user.getBraId());
		//创建财务明细对象
		SnFiDetail snFiDetail = new SnFiDetail();
		//给财务ID赋值
		snFiDetail.setFinId(snFinance.getFinId());
		//创建大数据对象
		BigDecimal allMoney = new BigDecimal(moneys);
		//给财务金额赋值
		snFiDetail.setFdMoney(allMoney);
		//给财务详情赋值
		snFiDetail.setFdExplain("批发商品增加财务金额"+allMoney+"元");
		//给操作人赋值
		snFiDetail.setOptime(Tools.getCurDateTime());
		//给是否有效赋值1
		snFiDetail.setIsva(1);
		//调用财务明细mapper增加财务明细的方法返回rows4
		int rows4 = snFiDetailMapper.insertSelective(snFiDetail);
		//如果全部成功返回rows否则返回0
		if(row>0 && rows>0 && rows2 > 0 && rows3 >0 && rows4>0){
			return rows;
		}else{
			return 0 ;
		}
		
	}

	@Override
	public List<SnCommodity> findAllProduction(Map<String, Object> maps) {
		return snCommodityMapper.findAllProduction(maps);
	}

	@Override
	public int findProductionCount(Map<String, Object> maps) {
		return snCommodityMapper.findProductionCount(maps);
	}

	@Override
	public int findInventory(String whId,HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//调用批发订单mapper根据批发订单ID查询批发商品明细的方法
		List<SuWholesale> list = suWholesaleMapper.findInventory(whId);
		//循环批发订单商品
		for (SuWholesale suWholesale : list) {
			//调用商品库存mapper根据分公司ID和商品ID查询商品库存数量的方法
			Integer number = comInventoryMapper.findWdNumber(user.getBraId(),suWholesale.getComId());
			//如果分公司商品库存数量小于批发订单商品的数量返回0否则返回1
			if(number<Integer.valueOf(suWholesale.getWdNumber())){
				return 0;
			}
		}
		return 1;
	}

	@Override
	public int refunds(String str,String whId,String result,HttpSession session) {
		//调用批发订单mapper根据批发订单ID修改批发订单状态返回rows
		int rows = suWholesaleMapper.updateStateByWhId(whId);
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//创建退货订单对象
		SnComSales comSales = new SnComSales();
		//给退货订单ID赋值
		comSales.setCsId(Tools.getDateOrderNo());
		//给退货订单公司ID赋值
		comSales.setBraId(user.getBraId());
		//给退货状态赋值0
		comSales.setCsState(0);
		//给退货订单时间赋值
		comSales.setOptime(Tools.getCurDateTime());
		//给是否有效赋值1
		comSales.setIsva(1);
		//给业务ID赋值
		comSales.setAllId(whId);
		//给操作人赋值
		comSales.setOper(user.getUserName());
		//给退货意见赋值
		comSales.setCsComment(result);
		//调用退货订单mapper批发退货订单增加的方法rows2
		int rows2 = snComSalesMapper.insertSelective(comSales);
		//创建审核对象
		SnAudit snAudit = new SnAudit();
		//给审核ID赋值
		snAudit.setAuId(Tools.getDateOrderNo());
		//给审核订单ID赋值
		snAudit.setAllId(comSales.getCsId());
		//给审核状态赋值0
		snAudit.setAuState(0);
		//调用审核mapper增加到审核表的方法返回rows3
		int rows3 = snAuditMapper.insertReturn(snAudit);
		//根据&号分割数据
		String[] string = str.split("&");
		//定义变量rows4
		int rows4= 0 ;
		//循环数据
		for (int i = 0; i < string.length; i++) {
			//把数据根据，分割成数组str2
			String[] str2 = string[i].split(",");
			//得到商品IdcomId 0
			String comId = str2[0];
			//得到商品原来数量number 1
			String number = str2[1];
			//得到退货的数量number2 2
			String number2 = str2[2];
			//创建退货详情表对象
			SnComSaDetail snComSaDetail = new SnComSaDetail();
			//给退货详情ID赋值
			snComSaDetail.setSdId(Tools.getDateOrderNo());
			//给退货订单ID赋值
			snComSaDetail.setCsId(comSales.getCsId());
			//给商品原来的数量赋值
			snComSaDetail.setSdAlready(Integer.parseInt(number));
			//给退货的数量赋值
			snComSaDetail.setSdCount(Integer.parseInt(number2));
			//给是否有效赋值1
			snComSaDetail.setIsva(1);
			//给商品ID（操作人）赋值
			snComSaDetail.setOper(comId);
			//给操作时间赋值
			snComSaDetail.setOptime(Tools.getCurDateTime());
			//调用退货订单明细mapper增加退货订单明细的方法返回rows4
			rows4 = snComSaDetailMapper.insertSelective(snComSaDetail);
		}
		//如果全部成功返回rows否则返回0
		if(rows>0 && rows2 > 0 && rows3 >0 && rows4>0){
			return rows;
		}else{
			return 0 ;
		}
	}

	@Override
	public List<SnAudit> findReturn(Map<String, Object> maps) {
		return snAuditMapper.findReturn(maps);
	}

	@Override
	public int findReturnCount(Map<String, Object> maps) {
		return snAuditMapper.findReturnCount(maps);
	}

	@Override
	public List<SnComSaDetail> findReturnDetail(String whId) {
		return snComSaDetailMapper.findReturnDetail(whId);
	}

	@Override
	public int findReturnDetailCount(String whId) {
		return snComSaDetailMapper.findReturnDetailCount(whId);
	}

	@Override
	public int returnAudit(SnAudit snAudit, HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//给操作人赋值
		snAudit.setOper(user.getUserName());
		//给操作时间赋值
		snAudit.setOptime(Tools.getCurDateTime());
		//调用批发订单mapper修改批发退货订单状态的方法返回rows2
		int rows2 = suWholesaleMapper.updateState(snAudit);
		//定义一个常量state等于五
		final int state= 5;
 		//如果审核状态等于state
		if(snAudit.getAuState()==state){
			//给审核状态赋值一
			snAudit.setAuState(1);
		}else {
			//给审核状态赋值二
			snAudit.setAuState(2);
		}
		//调用审核mapper批发退货订单审核的方法返回rows
		int rows = snAuditMapper.returnAudit(snAudit);
		//全部成功返回rows否则返回0
		if(rows > 0 && rows2 >0){
			return rows;
		}else{
			return 0;
		}
	}

	@Override
	public List<SnComSaDetail> findAuditDetail(String whId) {
		return snAuditMapper.findAuditDetail(whId);
	}

	@Override
	public int findAuditDetailCount(String whId) {
		return snAuditMapper.findAuditDetailCount(whId);
	}

	@Override
	public int findMoney(String whId, HttpSession session) {
		//得到session用户
		SnUser user = (SnUser) session.getAttribute("user");
		//调用批发订单mapper根据分公司ID查询财务总金额的方法
		Double money = suWholesaleMapper.findAllMoney(user.getBraId());
		//定义退货金额变量moneys
		Double moneys = 0.00;
		//定义退货总金额变量sumMoney
		Double sumMoney = 0.00;
		//调用退货订单明细mapper根据批发订单ID查询退货明细的方法
		List<SnComSaDetail> list = snComSaDetailMapper.findMoney(whId);
		//循环集合
		for (SnComSaDetail snComSaDetail : list) {
			//取到批发退货明细数量count
			int	count = snComSaDetail.getSdCount();
			//取到批发退货明细商品价格trade
			double trade = Double.parseDouble(snComSaDetail.getCiTrade());
			//给退货金额变量赋值
			moneys = (count*trade);
			//给退货总金额赋值
			sumMoney+=moneys;
		}
		//如果财务金额小于退货总金额返回0否则返回1
		if(money<sumMoney){
			return 0;
		}else{
			return 1;
		}
	}

	@Override
	public int returnedPurchase(String whId, HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//调用批发订单mapper修改批发状态的方法返回row
		int row = suWholesaleMapper.updateSort2(whId);
		//调用退货订单明细mapper根据批发订单ID查询商品退货明细
		List<SnComSaDetail> list = snComSaDetailMapper.findMoney(whId);
		//定义变量rows
		int rows = 0;
		//定义变量rows2
		int rows2 = 0 ;
		//定义总金额变量moneys
		double moneys = 0.0;
		//循环查询的数据
		for (SnComSaDetail snComSaDetail : list) {
			//调用商品库存mapper根据分公司ID和商品Id增加商品库存的方法返回rows
			rows = comInventoryMapper.addInventory(user.getBraId(),snComSaDetail.getComId(),snComSaDetail.getSdCount());
			//调用商品库存mapper根据分公司ID和商品ID查询商品库存ID的方法
			String inventoryId = comInventoryMapper.queryInventoryId(user.getBraId(),snComSaDetail.getComId());
			//创建库存明细对象
			SnComInDetail snComInDetail = new SnComInDetail();
			//给库存ID赋值
			snComInDetail.setCiId(inventoryId);
			//给库存明细赋值
			snComInDetail.setCidExplain("批发退货"+snComSaDetail.getComName()+"增加"+snComSaDetail.getComName()+"库存"+snComSaDetail.getSdCount()+snComSaDetail.getComUnit());
			//给是否有效赋值1
			snComInDetail.setIsva(1);
			//给操作时间赋值
			snComInDetail.setOptime(Tools.getCurDateTime());
			//调用商品库存明细mapper的增加库存明细的方法返回rows2
			rows2 = snComInDetailMapper.insertSelective(snComInDetail);
			//所有商品小计赋值给总金额
			moneys+=(snComSaDetail.getSdCount()*Double.valueOf(snComSaDetail.getCiTrade()));
		}
		//调用财务mapper根据分公司ID减少金钱的方法返回rows3
		int rows3 = snFinanceMapper.reduceMoney(user.getBraId(),moneys);
		//调用财务mapper根据分公司ID查询财务ID的方法
		SnFinance snFinance = snFinanceMapper.findByFinId(user.getBraId());
		//创建财务明细对象
		SnFiDetail snFiDetail = new SnFiDetail();
		//给财务ID赋值
		snFiDetail.setFinId(snFinance.getFinId());
		//创建大数据对象
		BigDecimal allMoney = new BigDecimal(moneys);
		//给财务金额赋值
		snFiDetail.setFdMoney(allMoney);
		//给财务详情赋值
		snFiDetail.setFdExplain("批发退货商品减少财务金额"+allMoney+"元");
		//给操作时间赋值
		snFiDetail.setOptime(Tools.getCurDateTime());
		//给是否有效赋值1
		snFiDetail.setIsva(1);
		//调用财务明细mapper的增加财务明细的方法返回rows4
		int rows4 = snFiDetailMapper.insertSelective(snFiDetail);
		//如果全部成功返回rows否则返回0
		if(row>0 && rows>0 && rows2 > 0 && rows3 >0 && rows4>0){
			return rows;
		}else{
			return 0;
		}
	}

}
